package com.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wms.dto.WarehouseDto;
import com.wms.dto.WarehouseIdAndNameDto;
import com.wms.dto.WarehousePageDto;
import com.wms.mapper.CityMapper;
import com.wms.mapper.WarehouseMapper;
import com.wms.pojo.City;
import com.wms.pojo.PageResult;
import com.wms.pojo.Warehouse;
import com.wms.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements WarehouseService {
    @Autowired
    private CityMapper cityMapper;
    @Override
    public PageResult pageQuery(WarehousePageDto dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        List<Warehouse> warehouses = getBaseMapper().selectList(Wrappers.<Warehouse>lambdaQuery()
                .like(ObjectUtil.isAllNotEmpty(dto.getName()), Warehouse::getName, dto.getName())
                .like(ObjectUtil.isAllNotEmpty(dto.getCityName()), Warehouse::getCityName, dto.getCityName())
                .like(ObjectUtil.isAllNotEmpty(dto.getPersonChargeName()), Warehouse::getPersonChargeName, dto.getPersonChargeName())
                .orderByAsc(Warehouse::getSort));
        Page<Warehouse> warehouses1 = (Page<Warehouse>) warehouses;
        return new PageResult(warehouses1.getTotal(),warehouses1.getResult());
    }

    @Override
    public void saveWarehouse(WarehouseDto dto) {
        City city1 = cityMapper.selectById(dto.getCityId().get(1));
        City city0 = cityMapper.selectById(dto.getCityId().get(0));
        Warehouse warehouse = Warehouse.builder().cityId(dto.getCityId().get(1))
                .provinceId(dto.getCityId().get(0))
                .cityName(city1.getName())
                .provinceName(city0.getName())
                .districtId(dto.getCityId().get(2))
                .address(dto.getAddress())
                .name(dto.getName())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .personChargeName(dto.getPersonChargeName())
                .createBy(dto.getPersonChargeName())
                .status(dto.getStatus())
                .build();
        getBaseMapper().insert(warehouse);
    }

    @Override
    public WarehouseDto getWarehouseById(Integer id) {
        Warehouse warehouse = getBaseMapper().selectById(id);
        List<Integer> list = new ArrayList<>();
        list.add(warehouse.getProvinceId());
        list.add(warehouse.getCityId());
        list.add(warehouse.getDistrictId());
        WarehouseDto warehouseDto = WarehouseDto.builder()
                .id(warehouse.getId())
                .address(warehouse.getAddress())
                .status(warehouse.getStatus())
                .name(warehouse.getName())
                .personChargeName(warehouse.getPersonChargeName())
                .cityId(list)
                .build();
        return warehouseDto;
    }

    @Override
    public void updateWarehouseById(WarehouseDto dto) {
        City city1 = cityMapper.selectById(dto.getCityId().get(1));
        City city0 = cityMapper.selectById(dto.getCityId().get(0));
        Warehouse warehouse = Warehouse.builder()
                .id(dto.getId())
                .cityId(dto.getCityId().get(1))
                .provinceId(dto.getCityId().get(0))
                .cityName(city1.getName())
                .provinceName(city0.getName())
                .districtId(dto.getCityId().get(2))
                .address(dto.getAddress())
                .name(dto.getName())
                .updateTime(LocalDateTime.now())
                .personChargeName(dto.getPersonChargeName())
                .createBy(dto.getPersonChargeName())
                .status(dto.getStatus())
                .build();
        getBaseMapper().updateById(warehouse);
    }

    @Override
    public List<WarehouseIdAndNameDto> getWarehouseAll() {
        List<Warehouse> warehouses = getBaseMapper().selectList(null);
        List<WarehouseIdAndNameDto> list = new ArrayList<>();
        for (Warehouse warehouse : warehouses) {
            Integer id = warehouse.getId();
            String name = warehouse.getName();
            WarehouseIdAndNameDto warehouseIdAndNameDto = new WarehouseIdAndNameDto();
            warehouseIdAndNameDto.setId(id);
            warehouseIdAndNameDto.setName(name);
            list.add(warehouseIdAndNameDto);
        }
        return list;
    }
}
